package com.rsxy.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rsxy.service.pojo.RsUser;
import com.rsxy.service.service.RsUserService;
import com.rsxy.service.tools.MD5Util;
import com.rsxy.service.tools.RedisUtil;
import com.rsxy.service.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags= "管理员相关接口")
public class UserController  {

    @Autowired
    private RsUserService rsUserService;
    @Autowired
    private RedisUtil redisUtil;
    @ApiOperation(value = "管理员登入",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value = "/login")
    public String login(HttpServletRequest req,String user, String pwd){
        Map<String, Object> map = new HashMap<>();
        map.put("user_password",MD5Util.md5(pwd));
        map.put("user_phone",user);
        List<RsUser> rsUser = rsUserService.getBaseMapper().selectByMap(map);
        if (rsUser.size()>0){
            JSONObject jsonObject = new JSONObject();
            redisUtil.set(user,jsonObject.toJSONString(rsUser.get(0)),3600*30);
            rsUserService.getBaseMapper().updateById(rsUser.get(0));
            //rsUserService.getBaseMapper().update(rsUser.get(0),null);
            return ResultUtil.success(200,"登入成功",rsUser.get(0));
        }else {
            return ResultUtil.success(200,"用户不存在",null);
        }
        // List<RsUser> rsUsers = rsUserService.getBaseMapper().selectByMap(map);
    }
}
